package com.itmake.sample.datascope.enums;

import cn.hutool.core.text.CharSequenceUtil;

/**
 * 数据范围枚举
 *
 * @author Amjacks
 * @since 2023/08/10
 */
public enum DataScopeEnum {

    DATA_SCOPE_ALL("1", "全部数据权限", ""),
    DATA_SCOPE_SELF("5", "仅本人数据权限",
            " {}user_id = {} ");

    private final String code;
    private final String remark;
    private final String sql;

    DataScopeEnum(String code, String remark, String sql) {
        this.code = code;
        this.remark = remark;
        this.sql = sql;
    }

    public String getCode() {
        return code;
    }

    public String getRemark() {
        return remark;
    }

    public String getSql() {
        return sql;
    }

    public static String buildSelf(String userAlias, String userId) {
        return CharSequenceUtil.format(DataScopeEnum.DATA_SCOPE_SELF.getSql(),
                buildAlias(userAlias), userId);
    }

    /**
     * 构建别名
     */
    private static String buildAlias(String alias) {
        if (CharSequenceUtil.isNotBlank(alias)) {
            return alias + ".";
        } else {
            return "";
        }
    }
}
